﻿using System;
using System.Collections;
using System.Collections.Generic;
using System.Configuration;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace DWConsoleTest
{
    public static class XMLConfigure
    {
        public static Configuration configuration = ConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel.None);
        /// <summary>
        /// 获取配置文件指定的Key
        /// </summary>
        /// <param name="key"></param>
        /// <returns></returns>
        public static string? YXHGetXMLConfigKey(this string key)
        {
            return configuration.AppSettings.Settings[key].Value;
        }
        /// <summary>
        /// 获取配置文件指定的section和key的value
        /// </summary>
        /// <param name="section"></param>
        /// <param name="key"></param>
        /// <returns>string</returns>
        public static string? YXHGetXMLConfigSection(this string section,string key)
        {
            try
            {
                IDictionary sectiondict = (IDictionary)ConfigurationManager.GetSection(section);
                string[] keys = new string[sectiondict.Keys.Count];
                string[] values = new string[sectiondict.Values.Count];
                sectiondict.Keys.CopyTo(keys, 0);
                sectiondict.Values.CopyTo(values, 0);
                if (keys.Contains(key))
                {
                    var keyindex = keys.ToList().IndexOf(key);
                    return values[keyindex];
                }
                else
                {
                    return null;
                }
            }
            catch (Exception ex)
            {

                Console.WriteLine($"此{section}下的{key}栏配置错误，请修正"+ex.Message);
                throw;
            }
           
        }

        /// <summary>
        /// converts a single CSV file to RSP format and saves it in a specified path, with indexing starting from 1.
        /// </summary>
        /// <param name="csvfilepath">csv file fullpath</param>
        /// <param name="chantitlelinenumber">line number where chanel titles can be found</param>
        ///  /// <param name="channumer">number of chanels</param>
        /// <param name="samplerate">samplerate of the data</param>
        /// <param name="excludecolumns">columns to exclude</param>
        /// <param name="savepath">The storage fullpath for RSP files</param>
        /// <returns></returns>
        //public static async Task CsvtoRSPAsync(this string csvfilepath, int chantitlelinenumber,int channumer,double samplerate,int[] excludecolumns,string savepath)
        //{
        //    //do something
        //}

    }
}
